package com.example.administrator.tenyuanpackage.speacial.util;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by yq on 2017/2/27.
 */

public class NetUtils {

    public static NetUtils instance;
    private IApi iapi;
    private  OkHttpClient okHttpClient;
    private  Retrofit retrofit;

    public static NetUtils getInstance(){
        if(instance==null){
            synchronized (NetUtils.class){
                if(instance==null){
                    instance=new NetUtils();
                }
            }
        }
        return instance;
    }

    private NetUtils(){
        okHttpClient = getClient();
        retrofit = getRetrofit();

    }
    public OkHttpClient getClient() {

        HttpLoggingInterceptor interceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("+++++++++++++++", message);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .connectTimeout(50, TimeUnit.SECONDS)
                .readTimeout(50,TimeUnit.SECONDS)
                .writeTimeout(50,TimeUnit.SECONDS)
                .build();
    }

    public Retrofit getRetrofit() {
        return new Retrofit.Builder()
                .client(okHttpClient)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .baseUrl(URLConfigs.baseURL)
                .build();
    }

    public IApi getApi(){
        if(iapi==null){
            iapi=retrofit.create(IApi.class);
        }
        return iapi;
    }


}
